﻿/// <reference path="knockout-2.2.1.js" />
/// <reference path="menu.js" />
/// <reference path="server.js" />
/// <reference path="jquery-2.0.2.min.js" />
/// <reference path="jquery-ui-1.10.3.js" />
/// <reference path="Utills.js" />


$(function() {
    utills.storegeObjectShim();
    //utills.prototypalOOP();
    // load menu data
    var menuData = server.getMenu();
    var ordersHistory = server.getOrdersHistory();
    // create menu items
    // var menuItem = new menu.MenuItem(menuData[0]);
    // show menu items - knockout.js functionality, it is hardcoded for now

    var viewModel = new MenuViewModel(menuData, ordersHistory);
    ko.applyBindings(viewModel);
    // start drag'n'drop
    $("#tabs").tabs();
    $("#menu").accordion({
        active: false,
        collapsible: true,
        heightStyle: "content"
    });
    prepareOrdersHistoryAccordeon();
    $("#menu .menuItem").draggable({
        appendTo: "body",
        helper: "clone"
    });
    $("#menu").on("click", ".menuItem", function(ev) {
        var item = $(ev.target);
        if (item.data("id") === undefined) {
            item = item.parent(".menuItem");
        }
        viewModel.changeInfo(item.data("id"));
        $("#additionalInfo").removeClass("hidden");
    });
    $(".slide").on("click", ".promotions", function(ev) {
        var item = $(ev.target);
        if (item.data("id") === undefined) {
            item = item.parent(".promotions");
        }   
        viewModel.changeInfo(item.data("id"));
        $("#additionalInfo").removeClass("hidden");
    });
    
    $("#order").droppable({
        activeClass: "ui-state-default",
        hoverClass: "ui-state-hover",
        accept: ":not(.ui-sortable-helper) ",
        drop: function(event, ui) {
            var droppedItem = $(ui.draggable[0]);
            var menuItemId = droppedItem.context.dataset.id;
            var historyOrderId = droppedItem.data("orderid");
            if (menuItemId) {
                viewModel.addOrderItem(parseInt(menuItemId));
            }
            else if (historyOrderId) {
                viewModel.reloadOrder(historyOrderId);
            }
        }
    });
    $("#clearOrder").on("click", viewModel.clearOrder);
    $("#clearHistory").on("click", function () {
        server.clearHistory();
        viewModel.ordersHistory([]);
    });

    $("#sendOrder").on("click", function() {
        var orderData = viewModel.getOrderData();
        viewModel.clearOrder;
        if (orderData.data.length === 0) {
            return;
        }
        server.sendOrder(orderData);
        server.saveOrder(orderData);
        viewModel.clearOrder();
        $("#history").accordion("destroy");
        viewModel.ordersHistory.push(orderData);
        setTimeout(prepareOrdersHistoryAccordeon, 10);
    });

    function prepareOrdersHistoryAccordeon() {
        $("#history").accordion({
            active: false,
            collapsible: true,
            heightStyle: "content"
        });
        $("#history .historyOrder").draggable({
            appendTo: "body",
            helper: "clone"
        });
    }
    $("#additionalInfo").draggable().find("#close").on("click", function() {
        $(this).parent("#additionalInfo").addClass("hidden");
    });
    $(document).ready(function() {
        $(".promotions").zoom();
    });
});